home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WINPROGS.ARJ / SYSMETS2.C < prev    next >
Text File  |  1990-11-23  |  5KB  |  170 lines

  1. /*   sysmets2.C   -  System Metrics Display Program No. 2  
  2.                      Petzold
  3. */
  4.  
  5. #include <windows.h>
  6. #include "sysmets.h"
  7.  
  8. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  9.  
  10. int PASCAL WinMain (HANDLE hInstance,
  11.                     HANDLE hPrevInstance,
  12.                     LPSTR  lpszCmdParam,
  13.                     int    nCmdShow)
  14.                     
  15.   {
  16.   static char szAppName[] = "sysmets2";
  17.   HWND        hwnd;
  18.   MSG         msg;
  19.   WNDCLASS    wndclass;
  20.   
  21.   if (!hPrevInstance)
  22.      {
  23.      wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  24.      wndclass.lpfnWndProc      = WndProc;
  25.      wndclass.cbClsExtra       = 0;
  26.      wndclass.cbWndExtra       = 0;
  27.      wndclass.hInstance        = hInstance;
  28.      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  29.      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  30.      wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  31.      wndclass.lpszMenuName     = NULL;
  32.      wndclass.lpszClassName    = szAppName;
  33.      
  34.      RegisterClass(&wndclass);
  35.      }
  36.      
  37.   hwnd = CreateWindow (szAppName,
  38.                        "Get System Metrics No. 2",
  39.                        WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
  40.                        CW_USEDEFAULT,   
  41.                        CW_USEDEFAULT,
  42.                        CW_USEDEFAULT,   
  43.                        CW_USEDEFAULT,   
  44.                        NULL,
  45.                        NULL,
  46.                        hInstance,
  47.                        NULL);
  48.                        
  49.   ShowWindow (hwnd, nCmdShow);
  50.   UpdateWindow (hwnd);
  51.   
  52.   while (GetMessage (&msg, NULL, 0, 0))
  53.     {
  54.     TranslateMessage (&msg);
  55.     DispatchMessage  (&msg);
  56.     }
  57.     
  58.   return msg.wParam;
  59.   }
  60.   
  61. long FAR PASCAL WndProc (HWND hwnd,
  62.                          WORD message,
  63.                          WORD wParam,
  64.                          LONG lParam)
  65.                          
  66.   {
  67.   static short cxChar, cxCaps, cyChar, cxClient, cyClient, nVscrollPos;
  68.   char         szBuffer[10];
  69.   short        i, y;
  70.   TEXTMETRIC   tm;
  71.   HDC          hdc;
  72.   PAINTSTRUCT  ps;
  73.   
  74.   switch (message)
  75.     {
  76.     case WM_CREATE:
  77.       hdc = GetDC (hwnd);
  78.       
  79.       GetTextMetrics (hdc, &tm);
  80.       cxChar = tm.tmAveCharWidth;
  81.       cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar /2;
  82.       cyChar = tm.tmHeight + tm.tmExternalLeading;
  83.       
  84.       ReleaseDC (hwnd, hdc);
  85.       
  86.       SetScrollRange (hwnd, SB_VERT, 0, NUMLINES, FALSE);
  87.       SetScrollPos (hwnd, SB_VERT, nVscrollPos, TRUE);
  88.       
  89.       return 0;
  90.       
  91.     case WM_SIZE:
  92.       cyClient = HIWORD (lParam);
  93.       cxClient = LOWORD (lParam);
  94.       return 0;
  95.       
  96.     case WM_VSCROLL:
  97.       switch (wParam)
  98.         {
  99.         case SB_LINEUP:
  100.           nVscrollPos -= 1;
  101.           break;
  102.           
  103.         case SB_LINEDOWN:
  104.           nVscrollPos += 1;
  105.           break;
  106.           
  107.         case SB_PAGEUP:
  108.           nVscrollPos -= cyClient / cyChar;
  109.           break;
  110.           
  111.         case SB_PAGEDOWN:
  112.           nVscrollPos += cyClient / cyChar;
  113.           break;
  114.           
  115.         case SB_THUMBPOSITION:
  116.           nVscrollPos = LOWORD (lParam);
  117.           break;
  118.           
  119.         default:
  120.           break;
  121.         }
  122.       nVscrollPos = max (0, min (nVscrollPos, NUMLINES));
  123.       
  124.       if (nVscrollPos != GetScrollPos (hwnd, SB_VERT))
  125.         {
  126.         SetScrollPos (hwnd, SB_VERT, nVscrollPos, TRUE);
  127.         InvalidateRect (hwnd, NULL, TRUE);
  128.         }
  129.       
  130.       return 0;
  131.     
  132.         
  133.     case WM_PAINT:
  134.       hdc = BeginPaint (hwnd, &ps);
  135.  
  136.       for (i = 0; i < NUMLINES; i++)
  137.         {
  138.         y = cyChar * ( 1 - nVscrollPos + i);
  139.         
  140.         TextOut (hdc, cxChar, y,
  141.                  sysmetrics[i].szLabel,
  142.                  lstrlen (sysmetrics[i].szLabel));
  143.  
  144.         TextOut (hdc, cxChar + 18 * cxCaps, y,
  145.                  sysmetrics[i].szDesc,
  146.                  lstrlen (sysmetrics[i].szDesc));
  147.                  
  148.          SetTextAlign (hdc, TA_RIGHT | TA_TOP);
  149.          
  150.          TextOut (hdc, cxChar + 18 * cxCaps + 40 * cxChar,
  151.                   y,
  152.                   szBuffer,
  153.                   wsprintf(szBuffer, "%5d",
  154.                            GetSystemMetrics (sysmetrics[i].nIndex)));
  155.                            
  156.          SetTextAlign (hdc, TA_LEFT | TA_TOP);                           
  157.          }
  158.          
  159.        EndPaint (hwnd, &ps);
  160.        return 0;
  161.          
  162.     case WM_DESTROY:
  163.       PostQuitMessage (0);
  164.       return 0;
  165.     }
  166.     
  167.   return DefWindowProc (hwnd, message, wParam, lParam);
  168.   }
  169.                   
  170.